package com.achcorp.sarch.core.domain;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
 */
@Entity
@Table
public class DocumentImp implements Document {

	private File storagePath;

	public DocumentImp(File storagePath) {
		this.storagePath = storagePath;
	}

	@Id
	@GeneratedValue
	@Column(name = "ID_DOC")
	private long id;

	@Column
	private String name;

	@Column
	private String description;

	@Column
	private Date dateCreation;

	@Column
	private String content;

	@ManyToMany(cascade = CascadeType.ALL, targetEntity = TagImp.class)
	@JoinTable(name = "document_tags", joinColumns = { @JoinColumn(name = "ID_DOC") }, inverseJoinColumns = { @JoinColumn(name = "ID_TAG") })
	private List<Tag> tags;

	@Override
	public String toString() {
		return "DocumentImp [id=" + id + ", name=" + name + "]";
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public File getContentFile() {
		return new File(storagePath, content);
	}

	public void addTag(Tag tag) {
		tags.add(tag);
	}

	public void removeTag(Tag tag) {
		tags.remove(tag);
	}
}
